<?php 
//初始化数据库运行
class firstClassAction extends ActionNot
{
	public function deltabAction()
	{
		$tabs 		= 'file,files';
		$deltable 	= explode(',',$tabs);
		foreach($deltable as $tab){
			$this->db->query('DROP TABLE `'.PREFIX.''.$tab.'`');
		}
		return ''.$tabs.'删除成功，再去<a href="?m=first">初始化</a>';
	}
	
	public function defaultAction()
	{

		$idkey	= '`id` INTEGER PRIMARY KEY AUTOINCREMENT,';
		$okey 	= '';
		if(DB_DRIVE!='sqlite'){
			$idkey 	= '`id` int(11) NOT NULL AUTO_INCREMENT,';
			$okey	= ',PRIMARY KEY (`id`)';
		}else{
			$this->rock->createdir(DB_HOST);
		}
		
		//平台用户,xinhuid信呼官网用户ID
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."users` (
			$idkey
			`user` varchar(50),
			`xinhuid` int(11) DEFAULT 0,
			`email` varchar(200),
			`name` varchar(50),
			`pass` varchar(50),
			`rand` varchar(10),
			`mobile` varchar(50),
			`face` varchar(200),
			`status` tinyint(1) DEFAULT 1,
			`type` tinyint(1) DEFAULT 0,
			`regdt` datetime DEFAULT NULL
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE UNIQUE INDEX user on `".PREFIX."users` (user)";
		$bo  = $this->db->query($sql, false);
		if($bo && getconfig('def_pass')){
			m('users')->insert(array(
				'user' => 'admin',
				'name' => '管理员',
				'pass' => md5(getconfig('def_pass')),
				'regdt'=> $this->rock->now
			));
		}
		
		
		//应用表,platid平台用户id
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."agent` (
			$idkey
			`name` varchar(50),
			`platid` int(11) DEFAULT 0,
			`agentkey` varchar(50) DEFAULT NULL,
			`url` varchar(200) DEFAULT NULL,
			`status` tinyint(1) DEFAULT '1',
			`optdt` datetime DEFAULT NULL
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE INDEX platid on `".PREFIX."agent` (platid)";
		$this->db->query($sql, false);
		
		//应用表的用户表uid第三方用户id
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."usera` (
			$idkey
			`name` varchar(50) DEFAULT NULL,
			`platid` int(11) DEFAULT 0,
			`uid` int(11) DEFAULT 0, 
			`agentid` int(11) DEFAULT 0,
			`face` varchar(50)
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE INDEX uid on `".PREFIX."usera` (uid)";
		$this->db->query($sql, false);
		
		
		//系统选项表
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."option` (
			$idkey
			`name` varchar(50) DEFAULT NULL,
			`pid` int(11) DEFAULT 0,
			`num` varchar(50) DEFAULT NULL ,
			`value` varchar(2000) DEFAULT NULL,
			`sort` smallint(6) DEFAULT '0' ,
			`valid` tinyint(1) DEFAULT '1',
			`optdt` datetime DEFAULT NULL,
			`optid` smallint(6) DEFAULT '0'
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE INDEX num on `".PREFIX."option` (num)";
		$this->db->query($sql, false);
		
		//创建file表
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."file` (
			$idkey
			`filename` varchar(200),
			`platid` int(11) DEFAULT 0,
			`fileid` int(11) DEFAULT 0,
			`agentid` int(11) DEFAULT 0,
			`filenum` varchar(200),
			`filepath` varchar(200),
			`filepathnew` varchar(200),
			`fileext` varchar(20),
			`optid` int(11) DEFAULT 0,
			`version` int(11) DEFAULT 1,
			`optname` varchar(50),
			`filesize` int(11) DEFAULT 0,
			`adddt` datetime DEFAULT NULL,
			`lastdt` datetime DEFAULT NULL,
			`lastid` int(11) DEFAULT 0
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE UNIQUE INDEX filenum on `".PREFIX."file` (filenum)";
		$this->db->query($sql, false);
		
		
		//创建files表，编辑记录表
		$sql = "CREATE TABLE IF NOT EXISTS `".PREFIX."files` (
			$idkey
			`mid` int(11) DEFAULT 0,
			`agentid` int(11) DEFAULT 0,
			`optid` int(11) DEFAULT 0,
			`platid` int(11) DEFAULT 0,
			`optname` varchar(100),
			`filepath` varchar(200),
			`filesize` int(11) DEFAULT 0,
			`version` int(11) DEFAULT 1,
			`optdt` datetime DEFAULT NULL,
			`isup` tinyint(1) DEFAULT '0',
			`explain` varchar(500)
			$okey
		)";
		$bo = $this->db->query($sql);
		if(!$bo)return $this->db->error();
		$sql = "CREATE INDEX mid on `".PREFIX."files` (mid)";
		$this->db->query($sql, false);
		
		
		return '欢迎使用，初始化完成，<a href="?m=index">去管理</a>';
	}
	
}